<?php

namespace app\admin\controller;

class Kingsvn extends \think\Controller {

    function __construct() {
        parent::__construct();
        $cookie = cookie('admin');
        if (!$cookie) {
            $this->redirect('common/login');
        } else {
            $list_admin_menu = $this->get_left_admin_menu_tree_root();
            $this->assign('list_admin_menu', $list_admin_menu);
        }
        $nav = $this->get_nav();
        $this->assign('nav', $nav);
    }

    // <editor-fold defaultstate="collapsed" desc="管理菜单树">

    protected function get_left_admin_menu_tree_root() {
        $list = db('admin_menu')->where('status', 1)->order('sort asc')->select();
        $array_tree = $this->get_left_admin_menu_tree($list);
        return $array_tree;
    }

    protected function get_left_admin_menu_tree($list, $pid = 0) {
        $list1 = array();
        foreach ($list as $m) {
            if ($m['p_id'] == $pid) {
                array_push($list1, $m);
            }
        }

        $array_tree = array();
        foreach ($list1 as $m) {
            $array = array();
            $array['id'] = $m['id'];
            if ($pid > 0) {
                $array['parent'] = $m['p_id'];
            }
            $array['name'] = $m['name'];
            $array['url'] = $m['url'];
            $array['icon'] = $m['icon'];

            $array_son = $this->get_left_admin_menu_tree($list, $m['id']);
            $array['children'] = array();
            if (count($array_son) > 0) {
                $array['children'] = $array_son;
            }
            array_push($array_tree, $array);
        }

        return $array_tree;
    }

// </editor-fold>
// 
// 
// <editor-fold defaultstate="collapsed" desc="整理当前导航显示">

    protected function get_nav() {
        //得到当前控制器和方法
        $request = request();
        $request->controller();
        $request->action();
        $url_str = $request->controller() . '/' . $request->action();
        //查询当前菜单
//        $where0['url'] = ['like', "%{$url_str}%"];
        $where0['url'] = $url_str;
        $menu = db('admin_menu')->where($where0)->order('depth desc')->limit(1)->find();

        //查询菜单树
        $str = $menu['tree'];
        $ids = substr($str, 0, strlen($str) - 1);
        $where['id'] = ['in', $ids];
        $list = db('admin_menu')->where($where)->order('tree asc')->select();
        //整理菜单成HTML代码
        $li = '';
        $count = count($list);
        for ($i = 0; $i < $count; $i++) {
            $icon = '<i class="' . $list[$i]['icon'] . '"></i>';
            $url = !empty($list[$i]['url']) ? url($list[$i]['url']) : '';
            $name = $list[$i]['name'];

            if ($count > 0) {
                if ($i === 0) {
                    $li.='<li>' . $icon . '<a href="' . $url . '">' . $name . '</a><i class="fa fa-angle-right"></i></li>';
                } else {
                    if ($i === ($count - 1)) {
                        $li.='<li><a href="' . $url . '">' . $name . '</a></li>';
                    } else {
                        $li.='<li><a href="' . $url . '">' . $name . '</a><i class="fa fa-angle-right"></i></li>';
                    }
                }
            } else {
                $li.='<li>' . $icon . '<a href="' . $url . '">' . $name . '</a></li>';
            }
        }

        return $li;
    }

    // </editor-fold>
}
